<template>
  <el-dialog
    title="批次管理"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible="visible"
    width="600px"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="批次名称" prop="batchName">
        <el-input
          v-model="dataForm.batchName"
          placeholder="请输入活动名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="生成数量" prop="createCount">
        <el-input
          v-model="dataForm.createCount"
          placeholder="请输入生成数量"
        ></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit"
        >确定</el-button
      >
    </span>

  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from "@/api/shop_operate/qrcode/batch";
export default {
  props:{
    visible: {
      type: Boolean,
      default: false
    },
  },
  data(){
    return{
      canSubmit:false,
      dataForm:{
        qrcodeId:''
      },
      dataRule:{
        batchName:[
          { required: true, message: "批次名称不能为空", trigger: "blur" },
        ],
        createCount:[
          { required: true, message: "生成个数不能为空", trigger: "blur" },
        ]
      }
    }
  },
  methods:{

    /**
     * 初始化
     */
    init(qrcodeId){
      this.canSubmit = true
      this.dataForm.qrcodeId = qrcodeId
    },

    /**
     * 提交
     */
    dataFormSubmit(){
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.canSubmit = false
          addObj(this.dataForm).then((data) => {
            this.$notify.success("添加成功");
            this.$emit("refreshDataList");
            this.closeDialog()
          }).catch(() => {
            this.canSubmit = true;
          });
        }
      })
    },

    //重置表单
    closeDialog() {
      this.$emit('update:visible', false)
    },
  }
}
</script>